using Vin.Extension.Workflow.Dto;
using Vin.Extension.Workflow.Node.Dto;
using Vin.Extension.Workflow.Nodes.Logic;
using Vin.Tool.Core.JsonCategory;

namespace Vin.Workflow.Tests;

public class TestNodes
{
    public static NodeDto CreateTestNode(string serviceKey, List<NodeParameterDto> inputParameterDtos, string id = "1",
        List<string>? nextNodeIds = null)
    {
        return new NodeDto()
        {
            Id = id,
            Name = serviceKey,
            ServiceKey = serviceKey,
            InputParameters = inputParameterDtos,
            NextNodeIds = nextNodeIds
        };
    }

    // 测试 http 节点
    public static NodeDto HttpNode => CreateTestNode("vin-http-node", new List<NodeParameterDto>()
    {
        new()
        {
            Key = "url",
            Value = "http://api.uomg.com/api/rand.qinghua",
            Type = ParameterType.String,
        },
        new()
        {
            Key = "method",
            Value = "GET",
            Type = ParameterType.String
        },
        new()
        {
            Key = "params",
            Value = new
            {
                format = "json"
            }.ToJson(),
            Type = ParameterType.Object
        },
    });

    // 测试json反序列化节点
    public static NodeDto DeserializeNode => CreateTestNode("vin-json-deserialize-node", new List<NodeParameterDto>()
    {
        new()
        {
            Key = "input",
            // Value = "{\"code\":1,\"content\":\"我没什么事儿，就想听听你的声音。\"}",
            Value = "123456789",
            // Value = "[{\"code\":1,\"content\":\"我没什么事儿，就想听听你的声音。\"},{\"code\":1,\"content\":\"我没什么事儿，就想听听你的声音。\"}]",
            Type = ParameterType.String,
        },
    });

    public static NodeDto ForNode => CreateTestNode("vin-for-node", new List<NodeParameterDto>()
    {
        new()
        {
            Key = "loopType",
            Value = "Count",
            Type = ParameterType.Enum,
        },
        new()
        {
            Key = "count",
            Value = "3",
            Type = ParameterType.Number,
        },
        new()
        {
            Key = "startNodeId",
            Value = "1",
            Type = ParameterType.String,
        },
        new()
        {
            Key = "nodes",
            Value = new List<NodeDto>()
            {
                CreateTestNode("vin-manual-input-node", new List<NodeParameterDto>()
                {
                    new()
                    {
                        Key = "test",
                        Value = "{{for-node-id._loopContext.Index}}"
                    }
                }, "1", new List<string>() { "2" }),
                // 创建if节点
                CreateTestNode("vin-if-node", new List<NodeParameterDto>()
                {
                    new()
                    {
                        Key = "conditionGroups",
                        Value = new List<ConditionGroupDto>()
                        {
                            new()
                            {
                                Conditions = new List<ConditionDto>()
                                {
                                    new()
                                    {
                                        A = new NodeParameterDto()
                                        {
                                            Type = ParameterType.Number,
                                            Value = "{{1.test}}"
                                        },
                                        Operator = ConditionOperator.Equals,
                                        B = new NodeParameterDto()
                                        {
                                            Type = ParameterType.Number,
                                            Value = "2"
                                        }
                                    },
                                },
                                NextNodeId = "continue"
                            }
                        }.ToJson(),
                        Type = ParameterType.Array
                    },
                    new()
                    {
                        Key = "defaultNextNodeId",
                        Value = "end",
                    }
                }, "2"),
                // Continue节点
                CreateTestNode("vin-for-continue-node", new List<NodeParameterDto>()
                {
                }, "continue"),
                // 默认输出节点
                CreateTestNode("vin-output-node", new List<NodeParameterDto>()
                {
                    new()
                    {
                        Key = "output",
                        Value = "循环第{{for-node-id._loopContext.Index}}次执行",
                        Type = ParameterType.String
                    }
                }, "end")
            }.ToJson(),
            Type = ParameterType.Array
        }
    }, "for-node-id");

    // 测试playwright的所有节点
    public static WorkflowDto PlaywrightWorkFlow => new WorkflowDto()
    {
        Id = "test-playwright-workflow",
        Name = "测试playwright的所有节点",
        StartNodeId = "start",
        EndNodeId = "end",
        Nodes =
        [
            // 启动浏览器
            CreateTestNode("vin-playwright-launch-browser-node", new List<NodeParameterDto>()
            {
                // new()
                // {
                //     Key = "debug",
                //     Type = ParameterType.Boolean,
                //     Value = "true"
                // },
                new()
                {
                    Key = "type",
                    Type = ParameterType.Enum,
                    Value = "Chromium"
                },
                new()
                {
                    Key = "headless",
                    Type = ParameterType.Boolean,
                    Value = "false"
                },
                new()
                {
                    Key = "executablePath",
                    Type = ParameterType.String,
                    Value = @"C:\Program Files\Google\Chrome\Application\chrome.exe"
                },
            }, "start", new List<string>() { "start1" }),
            // 启动浏览器
            CreateTestNode("vin-playwright-launch-browser-node", new List<NodeParameterDto>()
            {
                // new()
                // {
                //     Key = "debug",
                //     Type = ParameterType.Boolean,
                //     Value = "true"
                // },
                new()
                {
                    Key = "type",
                    Type = ParameterType.Enum,
                    Value = "Chromium"
                },
                new()
                {
                    Key = "headless",
                    Type = ParameterType.Boolean,
                    Value = "false"
                },
                new()
                {
                    Key = "executablePath",
                    Type = ParameterType.String,
                    Value = @"C:\Program Files\Google\Chrome\Application\chrome.exe"
                },
            }, "start1", new List<string>() { "1" }),
            // 创建浏览器上下文
            CreateTestNode("vin-playwright-create-context-node", new List<NodeParameterDto>()
            {
                new()
                {
                    Key = "browser",
                    Type = ParameterType.Object,
                    Value = "{{start.browser}}"
                }
            }, "1", new List<string>() { "2" }),
            // 打开新页面
            CreateTestNode("vin-playwright-open-page-node", new List<NodeParameterDto>()
            {
                new()
                {
                    Key = "context",
                    Type = ParameterType.Object,
                    Value = "{{1.context}}"
                },
                new()
                {
                    Key = "url",
                    Type = ParameterType.String,
                    Value = "https://www.baidu.com"
                }
            }, "2", new List<string>() { "3" }),
            // 创建选择器
            CreateTestNode("vin-playwright-create-locator-node", new List<NodeParameterDto>()
            {
                new()
                {
                    Key = "page",
                    Type = ParameterType.Object,
                    Value = "{{2.page}}"
                },
                new()
                {
                    Key = "selector",
                    Type = ParameterType.String,
                    // Value = "#chat-submit-button"
                    Value = ".hotsearch-item .title-content-title"
                }
            }, "3", new List<string>() { "4" }),
            // 点击选择器（单击）
            CreateTestNode("vin-playwright-locator-click-node", new List<NodeParameterDto>()
            {
                new()
                {
                    Key = "locator",
                    Type = ParameterType.Object,
                    Value = "{{3.first}}"
                },
                // new()
                // {
                //     Key = "dblClick",
                //     Type = ParameterType.Boolean,
                //     Value = "false"
                // }
                new()
                {
                    Key = "isCreateNewPage",
                    Type = ParameterType.Boolean,
                    Value = "true"
                }
            }, "4", new List<string>() { "5" }),
            // 获取最后创建的页面
            CreateTestNode("vin-playwright-context-pages-node", new List<NodeParameterDto>()
                {
                    new()
                    {
                        Key = "context",
                        Type = ParameterType.Object,
                        Value = "{{1.context}}"
                    }
                }, "5",
                new List<string>() { "6" }),
            // 创建选择器
            CreateTestNode("vin-playwright-create-locator-node",
                new List<NodeParameterDto>()
                {
                    new()
                    {
                        Key = "page",
                        Type = ParameterType.Object,
                        Value = "{{5.last}}"
                    },
                    new()
                    {
                        Key = "selector",
                        Type = ParameterType.String,
                        Value = ".summary_22rnB>span"
                    }
                },
                "6", new List<string>() { "7" }),
            // 点击选择器（双击）
            CreateTestNode("vin-playwright-locator-click-node", new List<NodeParameterDto>()
            {
                new()
                {
                    Key = "locator",
                    Type = ParameterType.Object,
                    Value = "{{6.first}}"
                },
                new()
                {
                    Key = "dblClick",
                    Type = ParameterType.Boolean,
                    Value = "true"
                }
            }, "7", new List<string>() { "end" }),
            // 输出结果
            CreateTestNode("vin-output-node", new()
            {
                new()
                {
                    Key = "count",
                    Type = ParameterType.String,
                    Value = "{{3.count}}"
                },
            }, "end")
        ]
    };
}